package com.zhouyibike.uu.ui.activity.hehuoren;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.interfaces.datasets.IDataSet;
import com.google.gson.Gson;
import com.zhouyibike.uu.R;
import com.zhouyibike.uu.entity.CityBikeCompanyStardantInterfaceResult;
import com.zhouyibike.uu.entity.HehuorenListResult;
import com.zhouyibike.uu.entity.WeekIncomingResult;

import java.util.ArrayList;

/**
 * Created by Zy on 2017/10/11.
 */
public class MyhehuorenFragment extends Fragment {
    private String tokenn;
    private int type = 0;
    private BarChart mChart;
    RelativeLayout jiazaily;
    LinearLayout nodataly;
    CityBikeCompanyStardantInterfaceResult model;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Bundle b = getArguments();
        View view = View.inflate(getActivity(), R.layout.fragment_hehuorenshouyitu, null);
        //  type=getIntent().getStringExtra("type");
        tokenn = b.getString("token");
        type = b.getInt("type");
        model= (CityBikeCompanyStardantInterfaceResult) b.getSerializable("model");
        initView(view);
        initCharts(view);
        jiazaily.setVisibility(View.VISIBLE);
//        if (type == 0) {
//            getMsg2("");
//        } else {
//            getMsg3("");
//        }
        getMsg2("");
        //  getMsg();
        // ((BaseActivity) getActivity()).toastShow(b.getString("type"));
        return view;
    }

    private void initView(View view) {
        jiazaily=view.findViewById(R.id.jiazaily);
        nodataly=view.findViewById(R.id.nodataly);
    }

    private void initCharts(View view) {
        mChart = (BarChart) view.findViewById(R.id.chart1);
        mChart.setDrawBarShadow(false);
        mChart.setDrawValueAboveBar(true);
        mChart.setDescription("");
        mChart.setMaxVisibleValueCount(60);
        mChart.setPinchZoom(false);
        mChart.setDrawGridBackground(false);
        mChart.setTouchEnabled(false);
        XAxis xAxis = mChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        //    xAxis.setTypeface(mTf);
        xAxis.setDrawGridLines(false);
        xAxis.setSpaceBetweenLabels(2);
        xAxis.setTextColor(getResources().getColor(R.color.radio_button_selected_color));
        xAxis.setTextSize(10f);
        xAxis.setAxisLineWidth(1f);
        xAxis.setAxisLineColor(getResources().getColor(R.color.bg_main_white));
        YAxis leftAxis = mChart.getAxisLeft();
        leftAxis.setTextSize(0f);
        leftAxis.setGridColor(Color.WHITE);
        leftAxis.setDrawGridLines(true);
        leftAxis.setLabelCount(8, false);
        leftAxis.setAxisLineWidth(1f);
        leftAxis.setAxisLineColor(Color.WHITE);
        leftAxis.setTextSize(0f);
        leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        leftAxis.setSpaceTop(15f);
        leftAxis.setAxisMinValue(0f);
        YAxis rightAxis = mChart.getAxisRight();
        rightAxis.setDrawGridLines(false);
        rightAxis.setLabelCount(8, false);
        rightAxis.setTextColor(Color.WHITE);
        rightAxis.setSpaceTop(15f);
        rightAxis.setAxisMinValue(0f);
        rightAxis.setEnabled(false);
        Legend l = mChart.getLegend();
        l.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT);
        l.setForm(Legend.LegendForm.SQUARE);
        l.setFormSize(9f);
        l.setTextSize(11f);
        l.setXEntrySpace(4f);
        l.setEnabled(false);
//        setData(7, 100);

    }

    private void getMsg2(String token) {
        setData(model);
        jiazaily.setVisibility(View.GONE);
    }

    public void datef(CityBikeCompanyStardantInterfaceResult model) {
        setData(model);
    }

    private void setData(CityBikeCompanyStardantInterfaceResult model) {

        ArrayList<String> xVals = new ArrayList<String>();
//        ArrayList<String> strings = getsevenday();
        if (model==null||model.getData()==null)
        {
            nodataly.setVisibility(View.VISIBLE);
        }else{
            nodataly.setVisibility(View.GONE);
        }

        ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
        if (type == 0) {
            for (int i = 0; i < model.getData().getWeekIncomingList().size(); i++) {
                xVals.add(new Gson().fromJson(model.getData().getWeekIncomingList().get(i),HehuorenListResult.class).getDate().split(" ")[0].substring(5));
            }
            for (int i = 0; i < model.getData().getWeekIncomingList().size(); i++) {
                float val = (float)new Gson().fromJson(model.getData().getWeekIncomingList().get(i),HehuorenListResult.class).getMoney();
                yVals1.add(new BarEntry(val, i));
            }
        } else {
            for (int i = 0; i < model.getData().getWeekBuyBikesList().size(); i++) {
                xVals.add(new Gson().fromJson(model.getData().getWeekBuyBikesList().get(i),HehuorenListResult.class).getDate().split(" ")[0].substring(5));
            }
            for (int i = 0; i < model.getData().getWeekBuyBikesList().size(); i++) {
                float val = (float) new Gson().fromJson(model.getData().getWeekBuyBikesList().get(i),HehuorenListResult.class).getMoney();
                yVals1.add(new BarEntry(val, i));
            }
        }
        BarDataSet set1;
        set1 = new BarDataSet(yVals1, "DataSet");
        set1.setBarSpacePercent(60f);
        set1.setColor(getResources().getColor(R.color.radio_button_selected_color));
        set1.setValueTextColor(getResources().getColor(R.color.radio_button_selected_color));
        ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
        dataSets.add(set1);
        BarData data = new BarData(xVals, dataSets);
        data.setValueTextSize(10f);
        mChart.setData(data);
        mChart.animateXY(0, 500);
//        for (IDataSet set : mChart.getData().getDataSets()) {
//            set.setValueTextColor(getResources().getColor(R.color.radio_button_selected_color));
//        }
        mChart.invalidate();
        mChart.setVisibility(View.VISIBLE);
    }
    private void setnullData(WeekIncomingResult model) {
        ArrayList<String> xVals = new ArrayList<String>();
//        ArrayList<String> strings = getsevenday();
        for (int i = 0; i < model.getData().size(); i++) {
            xVals.add(model.getData().get(i).getDay() + "");
        }
        ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
        for (int i = 0; i < model.getData().size(); i++) {
            float val = (float) model.getData().get(i).getAmount();
            yVals1.add(new BarEntry(val, i));
        }
        BarDataSet set1;
        set1 = new BarDataSet(yVals1, "DataSet");
        set1.setBarSpacePercent(50f);
        set1.setColor(getResources().getColor(R.color.radio_button_selected_color));
        ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
        dataSets.add(set1);
        BarData data = new BarData(xVals, dataSets);
        data.setValueTextSize(10f);
        mChart.setData(data);
        mChart.animateXY(0, 500);
        for (IDataSet set : mChart.getData().getDataSets()) {
            set.setValueTextColor(getResources().getColor(R.color.radio_button_selected_color));
        }
        mChart.invalidate();
        mChart.setVisibility(View.VISIBLE);
    }
}
